{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. [开发一个数据分析程序](#%E5%BC%80%E5%8F%91%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E7%A8%8B%E5%BA%8F)\n",
    "2. [通过改变数据类型节约内存](#%E9%80%9A%E8%BF%87%E6%94%B9%E5%8F%98%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%8A%82%E7%BA%A6%E5%86%85%E5%AD%98)\n",
    "3. [在最大值里选取最小值](#%E5%9C%A8%E6%9C%80%E5%A4%A7%E5%80%BC%E9%87%8C%E9%80%89%E5%8F%96%E6%9C%80%E5%B0%8F%E5%80%BC)\n",
    "4. [在每个分组选取最大值](#%E5%9C%A8%E6%AF%8F%E4%B8%AA%E5%88%86%E7%BB%84%E9%80%89%E5%8F%96%E6%9C%80%E5%A4%A7%E5%80%BC)\n",
    "5. [使用sort_values替换nlargest](#%E4%BD%BF%E7%94%A8sort_values%E6%9B%BF%E6%8D%A2nlargest)\n",
    "6. [计算止损价格](#%E8%AE%A1%E7%AE%97%E6%AD%A2%E6%8D%9F%E4%BB%B7%E6%A0%BC)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from IPython.display import display\n",
    "pd.options.display.max_columns = 50"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开发一个数据分析程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>STABBR</th>\n",
       "      <th>HBCU</th>\n",
       "      <th>MENONLY</th>\n",
       "      <th>WOMENONLY</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>SATVRMID</th>\n",
       "      <th>SATMTMID</th>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <th>UGDS</th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <th>CURROPER</th>\n",
       "      <th>PCTPELL</th>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <th>UG25ABV</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama A &amp; M University</td>\n",
       "      <td>Normal</td>\n",
       "      <td>AL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>424.0</td>\n",
       "      <td>420.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4206.0</td>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "      <td>0.0656</td>\n",
       "      <td>1</td>\n",
       "      <td>0.7356</td>\n",
       "      <td>0.8284</td>\n",
       "      <td>0.1049</td>\n",
       "      <td>30300</td>\n",
       "      <td>33888</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>University of Alabama at Birmingham</td>\n",
       "      <td>Birmingham</td>\n",
       "      <td>AL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>570.0</td>\n",
       "      <td>565.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11383.0</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.2607</td>\n",
       "      <td>1</td>\n",
       "      <td>0.3460</td>\n",
       "      <td>0.5214</td>\n",
       "      <td>0.2422</td>\n",
       "      <td>39700</td>\n",
       "      <td>21941.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Amridge University</td>\n",
       "      <td>Montgomery</td>\n",
       "      <td>AL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>291.0</td>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "      <td>0.4536</td>\n",
       "      <td>1</td>\n",
       "      <td>0.6801</td>\n",
       "      <td>0.7795</td>\n",
       "      <td>0.8540</td>\n",
       "      <td>40100</td>\n",
       "      <td>23370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>University of Alabama in Huntsville</td>\n",
       "      <td>Huntsville</td>\n",
       "      <td>AL</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>595.0</td>\n",
       "      <td>590.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5451.0</td>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "      <td>0.2146</td>\n",
       "      <td>1</td>\n",
       "      <td>0.3072</td>\n",
       "      <td>0.4596</td>\n",
       "      <td>0.2640</td>\n",
       "      <td>45500</td>\n",
       "      <td>24097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alabama State University</td>\n",
       "      <td>Montgomery</td>\n",
       "      <td>AL</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>425.0</td>\n",
       "      <td>430.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4811.0</td>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "      <td>0.0892</td>\n",
       "      <td>1</td>\n",
       "      <td>0.7347</td>\n",
       "      <td>0.7554</td>\n",
       "      <td>0.1270</td>\n",
       "      <td>26600</td>\n",
       "      <td>33118.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                INSTNM        CITY STABBR  HBCU  MENONLY  \\\n",
       "0             Alabama A & M University      Normal     AL   1.0      0.0   \n",
       "1  University of Alabama at Birmingham  Birmingham     AL   0.0      0.0   \n",
       "2                   Amridge University  Montgomery     AL   0.0      0.0   \n",
       "3  University of Alabama in Huntsville  Huntsville     AL   0.0      0.0   \n",
       "4             Alabama State University  Montgomery     AL   1.0      0.0   \n",
       "\n",
       "   WOMENONLY  RELAFFIL  SATVRMID  SATMTMID  DISTANCEONLY     UGDS  UGDS_WHITE  \\\n",
       "0        0.0         0     424.0     420.0           0.0   4206.0      0.0333   \n",
       "1        0.0         0     570.0     565.0           0.0  11383.0      0.5922   \n",
       "2        0.0         1       NaN       NaN           1.0    291.0      0.2990   \n",
       "3        0.0         0     595.0     590.0           0.0   5451.0      0.6988   \n",
       "4        0.0         0     425.0     430.0           0.0   4811.0      0.0158   \n",
       "\n",
       "   UGDS_BLACK  UGDS_HISP  UGDS_ASIAN  UGDS_AIAN  UGDS_NHPI  UGDS_2MOR  \\\n",
       "0      0.9353     0.0055      0.0019     0.0024     0.0019     0.0000   \n",
       "1      0.2600     0.0283      0.0518     0.0022     0.0007     0.0368   \n",
       "2      0.4192     0.0069      0.0034     0.0000     0.0000     0.0000   \n",
       "3      0.1255     0.0382      0.0376     0.0143     0.0002     0.0172   \n",
       "4      0.9208     0.0121      0.0019     0.0010     0.0006     0.0098   \n",
       "\n",
       "   UGDS_NRA  UGDS_UNKN  PPTUG_EF  CURROPER  PCTPELL  PCTFLOAN  UG25ABV  \\\n",
       "0    0.0059     0.0138    0.0656         1   0.7356    0.8284   0.1049   \n",
       "1    0.0179     0.0100    0.2607         1   0.3460    0.5214   0.2422   \n",
       "2    0.0000     0.2715    0.4536         1   0.6801    0.7795   0.8540   \n",
       "3    0.0332     0.0350    0.2146         1   0.3072    0.4596   0.2640   \n",
       "4    0.0243     0.0137    0.0892         1   0.7347    0.7554   0.1270   \n",
       "\n",
       "  MD_EARN_WNE_P10 GRAD_DEBT_MDN_SUPP  \n",
       "0           30300              33888  \n",
       "1           39700            21941.5  \n",
       "2           40100              23370  \n",
       "3           45500              24097  \n",
       "4           26600            33118.5  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college = pd.read_csv('data/college.csv')\n",
    "college.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7535, 27)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.923291</td>\n",
       "      <td>0.266146</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3578</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            count      mean       std  min     25%      50%       75%  max\n",
       "HBCU       7164.0  0.014238  0.118478  0.0  0.0000  0.00000  0.000000  1.0\n",
       "MENONLY    7164.0  0.009213  0.095546  0.0  0.0000  0.00000  0.000000  1.0\n",
       "WOMENONLY  7164.0  0.005304  0.072642  0.0  0.0000  0.00000  0.000000  1.0\n",
       "RELAFFIL   7535.0  0.190975  0.393096  0.0  0.0000  0.00000  0.000000  1.0\n",
       "...           ...       ...       ...  ...     ...      ...       ...  ...\n",
       "CURROPER   7535.0  0.923291  0.266146  0.0  1.0000  1.00000  1.000000  1.0\n",
       "PCTPELL    6849.0  0.530643  0.225544  0.0  0.3578  0.52150  0.712900  1.0\n",
       "PCTFLOAN   6849.0  0.522211  0.283616  0.0  0.3329  0.58330  0.745000  1.0\n",
       "UG25ABV    6718.0  0.410021  0.228939  0.0  0.2415  0.40075  0.572275  1.0\n",
       "\n",
       "[22 rows x 8 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "with 语句实质是上下文管理。\n",
    "1、上下文管理协议。包含方法__enter__() 和 __exit__()，支持该协议对象要实现这两个方法。\n",
    "2、上下文管理器，定义执行with语句时要建立的运行时上下文，负责执行with语句块上下文中的进入与退出操作。\n",
    "3、进入上下文的时候执行__enter__方法，如果设置as var语句，var变量接受__enter__()方法返回值。\n",
    "4、如果运行时发生了异常，就退出上下文管理器。调用管理器__exit__方法。\n",
    "\"\"\"\n",
    "with pd.option_context('display.max_rows', 8): # 调整显示设置价\n",
    "    display(college.describe(include=[np.number]).T) # 对所有数值类型进行统计然后转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter\n",
      "do_sth\n",
      "exit\n",
      "None None\n",
      "enter\n",
      "exit\n",
      "<class 'Exception'> do_exc\n"
     ]
    },
    {
     "ename": "Exception",
     "evalue": "do_exc",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mException\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-1a62aaa26a2c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     22\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mMyContext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'do_exc'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mmc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 24\u001b[1;33m     \u001b[0mmc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdo_exc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 会先接住这个异常，调用__exit__确保正常退出，然后再抛出异常。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-5-1a62aaa26a2c>\u001b[0m in \u001b[0;36mdo_exc\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mdo_exc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'do_exc'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mMyContext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'do_sth'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mmc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mException\u001b[0m: do_exc"
     ]
    }
   ],
   "source": [
    "# 关于with的例子，供感兴趣的同学参考。\n",
    "class MyContext(object):\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        \n",
    "    def __enter__(self): # 创建之后做一些准备工作，同时返回self，否则进入with的话就是None。可售试者把return去掉看看。\n",
    "        print('enter')\n",
    "        return self\n",
    "    \n",
    "    def __exit__(self, exc_type, exc_value,traceback):\n",
    "        print('exit')\n",
    "        print(exc_type, exc_value)\n",
    "    \n",
    "    def do_sth(self):\n",
    "        print('do_sth')\n",
    "    \n",
    "    def do_exc(self):\n",
    "        raise Exception('do_exc')\n",
    "        \n",
    "with MyContext('do_sth') as mc:\n",
    "    mc.do_sth()\n",
    "    \n",
    "with MyContext('do_exc') as mc:\n",
    "    mc.do_exc() # 会先接住这个异常，调用__exit__确保正常退出，然后再抛出异常。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7535 entries, 0 to 7534\n",
      "Data columns (total 27 columns):\n",
      " #   Column              Non-Null Count  Dtype  \n",
      "---  ------              --------------  -----  \n",
      " 0   INSTNM              7535 non-null   object \n",
      " 1   CITY                7535 non-null   object \n",
      " 2   STABBR              7535 non-null   object \n",
      " 3   HBCU                7164 non-null   float64\n",
      " 4   MENONLY             7164 non-null   float64\n",
      " 5   WOMENONLY           7164 non-null   float64\n",
      " 6   RELAFFIL            7535 non-null   int64  \n",
      " 7   SATVRMID            1185 non-null   float64\n",
      " 8   SATMTMID            1196 non-null   float64\n",
      " 9   DISTANCEONLY        7164 non-null   float64\n",
      " 10  UGDS                6874 non-null   float64\n",
      " 11  UGDS_WHITE          6874 non-null   float64\n",
      " 12  UGDS_BLACK          6874 non-null   float64\n",
      " 13  UGDS_HISP           6874 non-null   float64\n",
      " 14  UGDS_ASIAN          6874 non-null   float64\n",
      " 15  UGDS_AIAN           6874 non-null   float64\n",
      " 16  UGDS_NHPI           6874 non-null   float64\n",
      " 17  UGDS_2MOR           6874 non-null   float64\n",
      " 18  UGDS_NRA            6874 non-null   float64\n",
      " 19  UGDS_UNKN           6874 non-null   float64\n",
      " 20  PPTUG_EF            6853 non-null   float64\n",
      " 21  CURROPER            7535 non-null   int64  \n",
      " 22  PCTPELL             6849 non-null   float64\n",
      " 23  PCTFLOAN            6849 non-null   float64\n",
      " 24  UG25ABV             6718 non-null   float64\n",
      " 25  MD_EARN_WNE_P10     6413 non-null   object \n",
      " 26  GRAD_DEBT_MDN_SUPP  7503 non-null   object \n",
      "dtypes: float64(20), int64(2), object(5)\n",
      "memory usage: 1.4+ MB\n"
     ]
    }
   ],
   "source": [
    "college.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATVRMID</th>\n",
       "      <td>1185.0</td>\n",
       "      <td>522.819409</td>\n",
       "      <td>68.578862</td>\n",
       "      <td>290.0</td>\n",
       "      <td>475.000000</td>\n",
       "      <td>510.00000</td>\n",
       "      <td>555.000000</td>\n",
       "      <td>765.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATMTMID</th>\n",
       "      <td>1196.0</td>\n",
       "      <td>530.765050</td>\n",
       "      <td>73.469767</td>\n",
       "      <td>310.0</td>\n",
       "      <td>482.000000</td>\n",
       "      <td>520.00000</td>\n",
       "      <td>565.000000</td>\n",
       "      <td>785.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005583</td>\n",
       "      <td>0.074519</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>2356.837940</td>\n",
       "      <td>5474.275871</td>\n",
       "      <td>0.0</td>\n",
       "      <td>117.000000</td>\n",
       "      <td>412.50000</td>\n",
       "      <td>1929.500000</td>\n",
       "      <td>151558.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.510207</td>\n",
       "      <td>0.286958</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.267500</td>\n",
       "      <td>0.55570</td>\n",
       "      <td>0.747875</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.189997</td>\n",
       "      <td>0.224587</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036125</td>\n",
       "      <td>0.10005</td>\n",
       "      <td>0.257700</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.161635</td>\n",
       "      <td>0.221854</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.027600</td>\n",
       "      <td>0.07140</td>\n",
       "      <td>0.198875</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.033544</td>\n",
       "      <td>0.073777</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.01290</td>\n",
       "      <td>0.032700</td>\n",
       "      <td>0.9727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.013813</td>\n",
       "      <td>0.070196</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00260</td>\n",
       "      <td>0.007300</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.004569</td>\n",
       "      <td>0.033125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.9983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.023950</td>\n",
       "      <td>0.031288</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01750</td>\n",
       "      <td>0.033900</td>\n",
       "      <td>0.5333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.016086</td>\n",
       "      <td>0.050172</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.011700</td>\n",
       "      <td>0.9286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.045181</td>\n",
       "      <td>0.093440</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01430</td>\n",
       "      <td>0.045400</td>\n",
       "      <td>0.9027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <td>6853.0</td>\n",
       "      <td>0.226639</td>\n",
       "      <td>0.246470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.15040</td>\n",
       "      <td>0.376900</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.923291</td>\n",
       "      <td>0.266146</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.357800</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.332900</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.241500</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               count         mean          std    min         25%        50%  \\\n",
       "HBCU          7164.0     0.014238     0.118478    0.0    0.000000    0.00000   \n",
       "MENONLY       7164.0     0.009213     0.095546    0.0    0.000000    0.00000   \n",
       "WOMENONLY     7164.0     0.005304     0.072642    0.0    0.000000    0.00000   \n",
       "RELAFFIL      7535.0     0.190975     0.393096    0.0    0.000000    0.00000   \n",
       "SATVRMID      1185.0   522.819409    68.578862  290.0  475.000000  510.00000   \n",
       "SATMTMID      1196.0   530.765050    73.469767  310.0  482.000000  520.00000   \n",
       "DISTANCEONLY  7164.0     0.005583     0.074519    0.0    0.000000    0.00000   \n",
       "UGDS          6874.0  2356.837940  5474.275871    0.0  117.000000  412.50000   \n",
       "UGDS_WHITE    6874.0     0.510207     0.286958    0.0    0.267500    0.55570   \n",
       "UGDS_BLACK    6874.0     0.189997     0.224587    0.0    0.036125    0.10005   \n",
       "UGDS_HISP     6874.0     0.161635     0.221854    0.0    0.027600    0.07140   \n",
       "UGDS_ASIAN    6874.0     0.033544     0.073777    0.0    0.002500    0.01290   \n",
       "UGDS_AIAN     6874.0     0.013813     0.070196    0.0    0.000000    0.00260   \n",
       "UGDS_NHPI     6874.0     0.004569     0.033125    0.0    0.000000    0.00000   \n",
       "UGDS_2MOR     6874.0     0.023950     0.031288    0.0    0.000000    0.01750   \n",
       "UGDS_NRA      6874.0     0.016086     0.050172    0.0    0.000000    0.00000   \n",
       "UGDS_UNKN     6874.0     0.045181     0.093440    0.0    0.000000    0.01430   \n",
       "PPTUG_EF      6853.0     0.226639     0.246470    0.0    0.000000    0.15040   \n",
       "CURROPER      7535.0     0.923291     0.266146    0.0    1.000000    1.00000   \n",
       "PCTPELL       6849.0     0.530643     0.225544    0.0    0.357800    0.52150   \n",
       "PCTFLOAN      6849.0     0.522211     0.283616    0.0    0.332900    0.58330   \n",
       "UG25ABV       6718.0     0.410021     0.228939    0.0    0.241500    0.40075   \n",
       "\n",
       "                      75%          max  \n",
       "HBCU             0.000000       1.0000  \n",
       "MENONLY          0.000000       1.0000  \n",
       "WOMENONLY        0.000000       1.0000  \n",
       "RELAFFIL         0.000000       1.0000  \n",
       "SATVRMID       555.000000     765.0000  \n",
       "SATMTMID       565.000000     785.0000  \n",
       "DISTANCEONLY     0.000000       1.0000  \n",
       "UGDS          1929.500000  151558.0000  \n",
       "UGDS_WHITE       0.747875       1.0000  \n",
       "UGDS_BLACK       0.257700       1.0000  \n",
       "UGDS_HISP        0.198875       1.0000  \n",
       "UGDS_ASIAN       0.032700       0.9727  \n",
       "UGDS_AIAN        0.007300       1.0000  \n",
       "UGDS_NHPI        0.002500       0.9983  \n",
       "UGDS_2MOR        0.033900       0.5333  \n",
       "UGDS_NRA         0.011700       0.9286  \n",
       "UGDS_UNKN        0.045400       0.9027  \n",
       "PPTUG_EF         0.376900       1.0000  \n",
       "CURROPER         1.000000       1.0000  \n",
       "PCTPELL          0.712900       1.0000  \n",
       "PCTFLOAN         0.745000       1.0000  \n",
       "UG25ABV          0.572275       1.0000  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=[np.number]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>1%</th>\n",
       "      <th>5%</th>\n",
       "      <th>10%</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>90%</th>\n",
       "      <th>95%</th>\n",
       "      <th>99%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.3329</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>0.84752</td>\n",
       "      <td>0.89792</td>\n",
       "      <td>0.986368</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>0.0374</td>\n",
       "      <td>0.0899</td>\n",
       "      <td>0.2415</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>0.72666</td>\n",
       "      <td>0.80000</td>\n",
       "      <td>0.917383</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>22 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           count      mean       std  min      1%      5%     10%     25%  \\\n",
       "HBCU      7164.0  0.014238  0.118478  0.0  0.0000  0.0000  0.0000  0.0000   \n",
       "MENONLY   7164.0  0.009213  0.095546  0.0  0.0000  0.0000  0.0000  0.0000   \n",
       "...          ...       ...       ...  ...     ...     ...     ...     ...   \n",
       "PCTFLOAN  6849.0  0.522211  0.283616  0.0  0.0000  0.0000  0.0000  0.3329   \n",
       "UG25ABV   6718.0  0.410021  0.228939  0.0  0.0025  0.0374  0.0899  0.2415   \n",
       "\n",
       "              50%       75%      90%      95%       99%  max  \n",
       "HBCU      0.00000  0.000000  0.00000  0.00000  1.000000  1.0  \n",
       "MENONLY   0.00000  0.000000  0.00000  0.00000  0.000000  1.0  \n",
       "...           ...       ...      ...      ...       ...  ...  \n",
       "PCTFLOAN  0.58330  0.745000  0.84752  0.89792  0.986368  1.0  \n",
       "UG25ABV   0.40075  0.572275  0.72666  0.80000  0.917383  1.0  \n",
       "\n",
       "[22 rows x 14 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with pd.option_context('display.max_rows', 5): # 指定百分位数，默认是25%，50%和75%（即四分位）。\n",
    "    display(college.describe(include=[np.number], \n",
    "                 percentiles=[.01, .05, .10, .25, .5, .75, .9, .95, .99]).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column_name</th>\n",
       "      <th>description</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>INSTNM</td>\n",
       "      <td>Institution Name</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CITY</td>\n",
       "      <td>City Location</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>STABBR</td>\n",
       "      <td>State Abbreviation</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HBCU</td>\n",
       "      <td>Historically Black College or University</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>PCTFLOAN</td>\n",
       "      <td>Percent Students with federal loan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>UG25ABV</td>\n",
       "      <td>Percent Students Older than 25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>MD_EARN_WNE_P10</td>\n",
       "      <td>Median Earnings 10 years after enrollment</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>GRAD_DEBT_MDN_SUPP</td>\n",
       "      <td>Median debt of completers</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>27 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           column_name                                description\n",
       "0               INSTNM                           Institution Name\n",
       "1                 CITY                              City Location\n",
       "2               STABBR                         State Abbreviation\n",
       "3                 HBCU   Historically Black College or University\n",
       "..                 ...                                        ...\n",
       "23            PCTFLOAN         Percent Students with federal loan\n",
       "24             UG25ABV             Percent Students Older than 25\n",
       "25     MD_EARN_WNE_P10  Median Earnings 10 years after enrollment\n",
       "26  GRAD_DEBT_MDN_SUPP                  Median debt of completers\n",
       "\n",
       "[27 rows x 2 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "college_dd = pd.read_csv('data/college_data_dictionary.csv') # 每一列名称的解释\n",
    "with pd.option_context('display.max_rows', 8):\n",
    "    display(college_dd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过改变数据类型节约内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>SATMTMID</th>\n",
       "      <th>CURROPER</th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>STABBR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>420.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Alabama A &amp; M University</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>565.0</td>\n",
       "      <td>1</td>\n",
       "      <td>University of Alabama at Birmingham</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>Amridge University</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>590.0</td>\n",
       "      <td>1</td>\n",
       "      <td>University of Alabama in Huntsville</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>430.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Alabama State University</td>\n",
       "      <td>AL</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   RELAFFIL  SATMTMID  CURROPER                               INSTNM STABBR\n",
       "0         0     420.0         1             Alabama A & M University     AL\n",
       "1         0     565.0         1  University of Alabama at Birmingham     AL\n",
       "2         1       NaN         1                   Amridge University     AL\n",
       "3         0     590.0         1  University of Alabama in Huntsville     AL\n",
       "4         0     430.0         1             Alabama State University     AL"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college = pd.read_csv('data/college.csv')\n",
    "different_cols = ['RELAFFIL', 'SATMTMID', 'CURROPER', 'INSTNM', 'STABBR']\n",
    "col2 = college.loc[:, different_cols] # loc先行后列，':'代表选择所有行或列（根据位置决定），不清楚的话可以复习一下切片的概念。\n",
    "col2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RELAFFIL      int64\n",
       "SATMTMID    float64\n",
       "CURROPER      int64\n",
       "INSTNM       object\n",
       "STABBR       object\n",
       "dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index           64\n",
       "RELAFFIL     60280\n",
       "SATMTMID     60280\n",
       "CURROPER     60280\n",
       "INSTNM      449092\n",
       "STABBR      233585\n",
       "dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "original_mem = col2.memory_usage(deep=True) # 查看每一列的内存占用情况\n",
    "original_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RELAFFIL       int8\n",
       "SATMTMID    float64\n",
       "CURROPER      int64\n",
       "INSTNM       object\n",
       "STABBR       object\n",
       "dtype: object"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col2['RELAFFIL'] = col2['RELAFFIL'].astype(np.int8) # 原数据int8足够了，可以省内存。\n",
    "col2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "INSTNM    7535\n",
       "STABBR      59\n",
       "dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col2.select_dtypes(include=['object']).nunique() # 不重复的值的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RELAFFIL        int8\n",
       "SATMTMID     float64\n",
       "CURROPER       int64\n",
       "INSTNM        object\n",
       "STABBR      category\n",
       "dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col2['STABBR'] = col2['STABBR'].astype('category') # 因为STABBR只有59个不重复的值，使用category类型类似与enum，可以压缩数据存储量。\n",
    "col2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index           64\n",
       "RELAFFIL      7535\n",
       "SATMTMID     60280\n",
       "CURROPER     60280\n",
       "INSTNM      449551\n",
       "STABBR       10900\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_mem = col2.memory_usage(deep=True) # 注意，int64变成int8后，占用只有原来的1/8。object变成category的节约与不重复值的数量有关。\n",
    "new_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index       1.000000\n",
       "RELAFFIL    0.125000\n",
       "SATMTMID    1.000000\n",
       "CURROPER    1.000000\n",
       "INSTNM      1.001022\n",
       "STABBR      0.046664\n",
       "dtype: float64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_mem / original_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "college = pd.read_csv('data/college.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index           64\n",
       "CURROPER     60280\n",
       "INSTNM      449092\n",
       "dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college[['CURROPER', 'INSTNM']].memory_usage(deep=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index           64\n",
       "CURROPER     60280\n",
       "INSTNM      449145\n",
       "dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.loc[0, 'CURROPER'] = 10000000\n",
    "college.loc[0, 'INSTNM'] = college.loc[0, 'INSTNM'] + 'a'\n",
    "college[['CURROPER', 'INSTNM']].memory_usage(deep=True) # 加一个字符消耗的内存比想象得要多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "college['MENONLY'].dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Cannot convert non-finite values (NA or inf) to integer",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-da4a224f2f9a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mcollege\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'MENONLY'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'int8'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 有空值NA或inf无限大值，会导致转换失败。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\users\\lin\\appdata\\local\\programs\\python\\python38-32\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors)\u001b[0m\n\u001b[0;32m   5696\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5697\u001b[0m             \u001b[1;31m# else, only a single dtype is given\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 5698\u001b[1;33m             \u001b[0mnew_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_data\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   5699\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_data\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__finalize__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5700\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\lin\\appdata\\local\\programs\\python\\python38-32\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors)\u001b[0m\n\u001b[0;32m    580\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    581\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mbool\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mstr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"raise\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 582\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"astype\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    583\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    584\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\lin\\appdata\\local\\programs\\python\\python38-32\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mapply\u001b[1;34m(self, f, filter, **kwargs)\u001b[0m\n\u001b[0;32m    440\u001b[0m                 \u001b[0mapplied\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    441\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 442\u001b[1;33m                 \u001b[0mapplied\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    443\u001b[0m             \u001b[0mresult_blocks\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_extend_blocks\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mapplied\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult_blocks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    444\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\lin\\appdata\\local\\programs\\python\\python38-32\\lib\\site-packages\\pandas\\core\\internals\\blocks.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors)\u001b[0m\n\u001b[0;32m    623\u001b[0m             \u001b[0mvals1d\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    624\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 625\u001b[1;33m                 \u001b[0mvalues\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mastype_nansafe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvals1d\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    626\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mValueError\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    627\u001b[0m                 \u001b[1;31m# e.g. astype_nansafe can fail on object-dtype of strings\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\lin\\appdata\\local\\programs\\python\\python38-32\\lib\\site-packages\\pandas\\core\\dtypes\\cast.py\u001b[0m in \u001b[0;36mastype_nansafe\u001b[1;34m(arr, dtype, copy, skipna)\u001b[0m\n\u001b[0;32m    866\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    867\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misfinite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 868\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Cannot convert non-finite values (NA or inf) to integer\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    869\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    870\u001b[0m     \u001b[1;32melif\u001b[0m \u001b[0mis_object_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Cannot convert non-finite values (NA or inf) to integer"
     ]
    }
   ],
   "source": [
    "college['MENONLY'].astype('int8') # 有空值NA或inf无限大值，会导致转换失败。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATVRMID</th>\n",
       "      <td>1185.0</td>\n",
       "      <td>522.819409</td>\n",
       "      <td>68.578862</td>\n",
       "      <td>290.0</td>\n",
       "      <td>475.000000</td>\n",
       "      <td>510.00000</td>\n",
       "      <td>555.000000</td>\n",
       "      <td>7.650000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATMTMID</th>\n",
       "      <td>1196.0</td>\n",
       "      <td>530.765050</td>\n",
       "      <td>73.469767</td>\n",
       "      <td>310.0</td>\n",
       "      <td>482.000000</td>\n",
       "      <td>520.00000</td>\n",
       "      <td>565.000000</td>\n",
       "      <td>7.850000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005583</td>\n",
       "      <td>0.074519</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>2356.837940</td>\n",
       "      <td>5474.275871</td>\n",
       "      <td>0.0</td>\n",
       "      <td>117.000000</td>\n",
       "      <td>412.50000</td>\n",
       "      <td>1929.500000</td>\n",
       "      <td>1.515580e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.510207</td>\n",
       "      <td>0.286958</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.267500</td>\n",
       "      <td>0.55570</td>\n",
       "      <td>0.747875</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.189997</td>\n",
       "      <td>0.224587</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036125</td>\n",
       "      <td>0.10005</td>\n",
       "      <td>0.257700</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.161635</td>\n",
       "      <td>0.221854</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.027600</td>\n",
       "      <td>0.07140</td>\n",
       "      <td>0.198875</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.033544</td>\n",
       "      <td>0.073777</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.01290</td>\n",
       "      <td>0.032700</td>\n",
       "      <td>9.727000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.013813</td>\n",
       "      <td>0.070196</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00260</td>\n",
       "      <td>0.007300</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.004569</td>\n",
       "      <td>0.033125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>9.983000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.023950</td>\n",
       "      <td>0.031288</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01750</td>\n",
       "      <td>0.033900</td>\n",
       "      <td>5.333000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.016086</td>\n",
       "      <td>0.050172</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.011700</td>\n",
       "      <td>9.286000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.045181</td>\n",
       "      <td>0.093440</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01430</td>\n",
       "      <td>0.045400</td>\n",
       "      <td>9.027000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <td>6853.0</td>\n",
       "      <td>0.226639</td>\n",
       "      <td>0.246470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.15040</td>\n",
       "      <td>0.376900</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>1328.063172</td>\n",
       "      <td>115201.552429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.357800</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.332900</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.241500</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               count         mean            std    min         25%  \\\n",
       "HBCU          7164.0     0.014238       0.118478    0.0    0.000000   \n",
       "MENONLY       7164.0     0.009213       0.095546    0.0    0.000000   \n",
       "WOMENONLY     7164.0     0.005304       0.072642    0.0    0.000000   \n",
       "RELAFFIL      7535.0     0.190975       0.393096    0.0    0.000000   \n",
       "SATVRMID      1185.0   522.819409      68.578862  290.0  475.000000   \n",
       "SATMTMID      1196.0   530.765050      73.469767  310.0  482.000000   \n",
       "DISTANCEONLY  7164.0     0.005583       0.074519    0.0    0.000000   \n",
       "UGDS          6874.0  2356.837940    5474.275871    0.0  117.000000   \n",
       "UGDS_WHITE    6874.0     0.510207       0.286958    0.0    0.267500   \n",
       "UGDS_BLACK    6874.0     0.189997       0.224587    0.0    0.036125   \n",
       "UGDS_HISP     6874.0     0.161635       0.221854    0.0    0.027600   \n",
       "UGDS_ASIAN    6874.0     0.033544       0.073777    0.0    0.002500   \n",
       "UGDS_AIAN     6874.0     0.013813       0.070196    0.0    0.000000   \n",
       "UGDS_NHPI     6874.0     0.004569       0.033125    0.0    0.000000   \n",
       "UGDS_2MOR     6874.0     0.023950       0.031288    0.0    0.000000   \n",
       "UGDS_NRA      6874.0     0.016086       0.050172    0.0    0.000000   \n",
       "UGDS_UNKN     6874.0     0.045181       0.093440    0.0    0.000000   \n",
       "PPTUG_EF      6853.0     0.226639       0.246470    0.0    0.000000   \n",
       "CURROPER      7535.0  1328.063172  115201.552429    0.0    1.000000   \n",
       "PCTPELL       6849.0     0.530643       0.225544    0.0    0.357800   \n",
       "PCTFLOAN      6849.0     0.522211       0.283616    0.0    0.332900   \n",
       "UG25ABV       6718.0     0.410021       0.228939    0.0    0.241500   \n",
       "\n",
       "                    50%          75%           max  \n",
       "HBCU            0.00000     0.000000  1.000000e+00  \n",
       "MENONLY         0.00000     0.000000  1.000000e+00  \n",
       "WOMENONLY       0.00000     0.000000  1.000000e+00  \n",
       "RELAFFIL        0.00000     0.000000  1.000000e+00  \n",
       "SATVRMID      510.00000   555.000000  7.650000e+02  \n",
       "SATMTMID      520.00000   565.000000  7.850000e+02  \n",
       "DISTANCEONLY    0.00000     0.000000  1.000000e+00  \n",
       "UGDS          412.50000  1929.500000  1.515580e+05  \n",
       "UGDS_WHITE      0.55570     0.747875  1.000000e+00  \n",
       "UGDS_BLACK      0.10005     0.257700  1.000000e+00  \n",
       "UGDS_HISP       0.07140     0.198875  1.000000e+00  \n",
       "UGDS_ASIAN      0.01290     0.032700  9.727000e-01  \n",
       "UGDS_AIAN       0.00260     0.007300  1.000000e+00  \n",
       "UGDS_NHPI       0.00000     0.002500  9.983000e-01  \n",
       "UGDS_2MOR       0.01750     0.033900  5.333000e-01  \n",
       "UGDS_NRA        0.00000     0.011700  9.286000e-01  \n",
       "UGDS_UNKN       0.01430     0.045400  9.027000e-01  \n",
       "PPTUG_EF        0.15040     0.376900  1.000000e+00  \n",
       "CURROPER        1.00000     1.000000  1.000000e+07  \n",
       "PCTPELL         0.52150     0.712900  1.000000e+00  \n",
       "PCTFLOAN        0.58330     0.745000  1.000000e+00  \n",
       "UG25ABV         0.40075     0.572275  1.000000e+00  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=['int64', 'float64']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATVRMID</th>\n",
       "      <td>1185.0</td>\n",
       "      <td>522.819409</td>\n",
       "      <td>68.578862</td>\n",
       "      <td>290.0</td>\n",
       "      <td>475.000000</td>\n",
       "      <td>510.00000</td>\n",
       "      <td>555.000000</td>\n",
       "      <td>7.650000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATMTMID</th>\n",
       "      <td>1196.0</td>\n",
       "      <td>530.765050</td>\n",
       "      <td>73.469767</td>\n",
       "      <td>310.0</td>\n",
       "      <td>482.000000</td>\n",
       "      <td>520.00000</td>\n",
       "      <td>565.000000</td>\n",
       "      <td>7.850000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005583</td>\n",
       "      <td>0.074519</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>2356.837940</td>\n",
       "      <td>5474.275871</td>\n",
       "      <td>0.0</td>\n",
       "      <td>117.000000</td>\n",
       "      <td>412.50000</td>\n",
       "      <td>1929.500000</td>\n",
       "      <td>1.515580e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.510207</td>\n",
       "      <td>0.286958</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.267500</td>\n",
       "      <td>0.55570</td>\n",
       "      <td>0.747875</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.189997</td>\n",
       "      <td>0.224587</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036125</td>\n",
       "      <td>0.10005</td>\n",
       "      <td>0.257700</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.161635</td>\n",
       "      <td>0.221854</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.027600</td>\n",
       "      <td>0.07140</td>\n",
       "      <td>0.198875</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.033544</td>\n",
       "      <td>0.073777</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.01290</td>\n",
       "      <td>0.032700</td>\n",
       "      <td>9.727000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.013813</td>\n",
       "      <td>0.070196</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00260</td>\n",
       "      <td>0.007300</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.004569</td>\n",
       "      <td>0.033125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>9.983000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.023950</td>\n",
       "      <td>0.031288</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01750</td>\n",
       "      <td>0.033900</td>\n",
       "      <td>5.333000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.016086</td>\n",
       "      <td>0.050172</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.011700</td>\n",
       "      <td>9.286000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.045181</td>\n",
       "      <td>0.093440</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01430</td>\n",
       "      <td>0.045400</td>\n",
       "      <td>9.027000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <td>6853.0</td>\n",
       "      <td>0.226639</td>\n",
       "      <td>0.246470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.15040</td>\n",
       "      <td>0.376900</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>1328.063172</td>\n",
       "      <td>115201.552429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.357800</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.332900</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.241500</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               count         mean            std    min         25%  \\\n",
       "HBCU          7164.0     0.014238       0.118478    0.0    0.000000   \n",
       "MENONLY       7164.0     0.009213       0.095546    0.0    0.000000   \n",
       "WOMENONLY     7164.0     0.005304       0.072642    0.0    0.000000   \n",
       "RELAFFIL      7535.0     0.190975       0.393096    0.0    0.000000   \n",
       "SATVRMID      1185.0   522.819409      68.578862  290.0  475.000000   \n",
       "SATMTMID      1196.0   530.765050      73.469767  310.0  482.000000   \n",
       "DISTANCEONLY  7164.0     0.005583       0.074519    0.0    0.000000   \n",
       "UGDS          6874.0  2356.837940    5474.275871    0.0  117.000000   \n",
       "UGDS_WHITE    6874.0     0.510207       0.286958    0.0    0.267500   \n",
       "UGDS_BLACK    6874.0     0.189997       0.224587    0.0    0.036125   \n",
       "UGDS_HISP     6874.0     0.161635       0.221854    0.0    0.027600   \n",
       "UGDS_ASIAN    6874.0     0.033544       0.073777    0.0    0.002500   \n",
       "UGDS_AIAN     6874.0     0.013813       0.070196    0.0    0.000000   \n",
       "UGDS_NHPI     6874.0     0.004569       0.033125    0.0    0.000000   \n",
       "UGDS_2MOR     6874.0     0.023950       0.031288    0.0    0.000000   \n",
       "UGDS_NRA      6874.0     0.016086       0.050172    0.0    0.000000   \n",
       "UGDS_UNKN     6874.0     0.045181       0.093440    0.0    0.000000   \n",
       "PPTUG_EF      6853.0     0.226639       0.246470    0.0    0.000000   \n",
       "CURROPER      7535.0  1328.063172  115201.552429    0.0    1.000000   \n",
       "PCTPELL       6849.0     0.530643       0.225544    0.0    0.357800   \n",
       "PCTFLOAN      6849.0     0.522211       0.283616    0.0    0.332900   \n",
       "UG25ABV       6718.0     0.410021       0.228939    0.0    0.241500   \n",
       "\n",
       "                    50%          75%           max  \n",
       "HBCU            0.00000     0.000000  1.000000e+00  \n",
       "MENONLY         0.00000     0.000000  1.000000e+00  \n",
       "WOMENONLY       0.00000     0.000000  1.000000e+00  \n",
       "RELAFFIL        0.00000     0.000000  1.000000e+00  \n",
       "SATVRMID      510.00000   555.000000  7.650000e+02  \n",
       "SATMTMID      520.00000   565.000000  7.850000e+02  \n",
       "DISTANCEONLY    0.00000     0.000000  1.000000e+00  \n",
       "UGDS          412.50000  1929.500000  1.515580e+05  \n",
       "UGDS_WHITE      0.55570     0.747875  1.000000e+00  \n",
       "UGDS_BLACK      0.10005     0.257700  1.000000e+00  \n",
       "UGDS_HISP       0.07140     0.198875  1.000000e+00  \n",
       "UGDS_ASIAN      0.01290     0.032700  9.727000e-01  \n",
       "UGDS_AIAN       0.00260     0.007300  1.000000e+00  \n",
       "UGDS_NHPI       0.00000     0.002500  9.983000e-01  \n",
       "UGDS_2MOR       0.01750     0.033900  5.333000e-01  \n",
       "UGDS_NRA        0.00000     0.011700  9.286000e-01  \n",
       "UGDS_UNKN       0.01430     0.045400  9.027000e-01  \n",
       "PPTUG_EF        0.15040     0.376900  1.000000e+00  \n",
       "CURROPER        1.00000     1.000000  1.000000e+07  \n",
       "PCTPELL         0.52150     0.712900  1.000000e+00  \n",
       "PCTFLOAN        0.58330     0.745000  1.000000e+00  \n",
       "UG25ABV         0.40075     0.572275  1.000000e+00  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=[np.int64, np.float64]).T # 跟上面一行等价，个人建议用np.xxx表示数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATVRMID</th>\n",
       "      <td>1185.0</td>\n",
       "      <td>522.819409</td>\n",
       "      <td>68.578862</td>\n",
       "      <td>290.0</td>\n",
       "      <td>475.000000</td>\n",
       "      <td>510.00000</td>\n",
       "      <td>555.000000</td>\n",
       "      <td>765.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATMTMID</th>\n",
       "      <td>1196.0</td>\n",
       "      <td>530.765050</td>\n",
       "      <td>73.469767</td>\n",
       "      <td>310.0</td>\n",
       "      <td>482.000000</td>\n",
       "      <td>520.00000</td>\n",
       "      <td>565.000000</td>\n",
       "      <td>785.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005583</td>\n",
       "      <td>0.074519</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>2356.837940</td>\n",
       "      <td>5474.275871</td>\n",
       "      <td>0.0</td>\n",
       "      <td>117.000000</td>\n",
       "      <td>412.50000</td>\n",
       "      <td>1929.500000</td>\n",
       "      <td>151558.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.510207</td>\n",
       "      <td>0.286958</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.267500</td>\n",
       "      <td>0.55570</td>\n",
       "      <td>0.747875</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.189997</td>\n",
       "      <td>0.224587</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036125</td>\n",
       "      <td>0.10005</td>\n",
       "      <td>0.257700</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.161635</td>\n",
       "      <td>0.221854</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.027600</td>\n",
       "      <td>0.07140</td>\n",
       "      <td>0.198875</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.033544</td>\n",
       "      <td>0.073777</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.01290</td>\n",
       "      <td>0.032700</td>\n",
       "      <td>0.9727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.013813</td>\n",
       "      <td>0.070196</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00260</td>\n",
       "      <td>0.007300</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.004569</td>\n",
       "      <td>0.033125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.9983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.023950</td>\n",
       "      <td>0.031288</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01750</td>\n",
       "      <td>0.033900</td>\n",
       "      <td>0.5333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.016086</td>\n",
       "      <td>0.050172</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.011700</td>\n",
       "      <td>0.9286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.045181</td>\n",
       "      <td>0.093440</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01430</td>\n",
       "      <td>0.045400</td>\n",
       "      <td>0.9027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <td>6853.0</td>\n",
       "      <td>0.226639</td>\n",
       "      <td>0.246470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.15040</td>\n",
       "      <td>0.376900</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.357800</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.332900</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.241500</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               count         mean          std    min         25%        50%  \\\n",
       "HBCU          7164.0     0.014238     0.118478    0.0    0.000000    0.00000   \n",
       "MENONLY       7164.0     0.009213     0.095546    0.0    0.000000    0.00000   \n",
       "WOMENONLY     7164.0     0.005304     0.072642    0.0    0.000000    0.00000   \n",
       "SATVRMID      1185.0   522.819409    68.578862  290.0  475.000000  510.00000   \n",
       "SATMTMID      1196.0   530.765050    73.469767  310.0  482.000000  520.00000   \n",
       "DISTANCEONLY  7164.0     0.005583     0.074519    0.0    0.000000    0.00000   \n",
       "UGDS          6874.0  2356.837940  5474.275871    0.0  117.000000  412.50000   \n",
       "UGDS_WHITE    6874.0     0.510207     0.286958    0.0    0.267500    0.55570   \n",
       "UGDS_BLACK    6874.0     0.189997     0.224587    0.0    0.036125    0.10005   \n",
       "UGDS_HISP     6874.0     0.161635     0.221854    0.0    0.027600    0.07140   \n",
       "UGDS_ASIAN    6874.0     0.033544     0.073777    0.0    0.002500    0.01290   \n",
       "UGDS_AIAN     6874.0     0.013813     0.070196    0.0    0.000000    0.00260   \n",
       "UGDS_NHPI     6874.0     0.004569     0.033125    0.0    0.000000    0.00000   \n",
       "UGDS_2MOR     6874.0     0.023950     0.031288    0.0    0.000000    0.01750   \n",
       "UGDS_NRA      6874.0     0.016086     0.050172    0.0    0.000000    0.00000   \n",
       "UGDS_UNKN     6874.0     0.045181     0.093440    0.0    0.000000    0.01430   \n",
       "PPTUG_EF      6853.0     0.226639     0.246470    0.0    0.000000    0.15040   \n",
       "PCTPELL       6849.0     0.530643     0.225544    0.0    0.357800    0.52150   \n",
       "PCTFLOAN      6849.0     0.522211     0.283616    0.0    0.332900    0.58330   \n",
       "UG25ABV       6718.0     0.410021     0.228939    0.0    0.241500    0.40075   \n",
       "\n",
       "                      75%          max  \n",
       "HBCU             0.000000       1.0000  \n",
       "MENONLY          0.000000       1.0000  \n",
       "WOMENONLY        0.000000       1.0000  \n",
       "SATVRMID       555.000000     765.0000  \n",
       "SATMTMID       565.000000     785.0000  \n",
       "DISTANCEONLY     0.000000       1.0000  \n",
       "UGDS          1929.500000  151558.0000  \n",
       "UGDS_WHITE       0.747875       1.0000  \n",
       "UGDS_BLACK       0.257700       1.0000  \n",
       "UGDS_HISP        0.198875       1.0000  \n",
       "UGDS_ASIAN       0.032700       0.9727  \n",
       "UGDS_AIAN        0.007300       1.0000  \n",
       "UGDS_NHPI        0.002500       0.9983  \n",
       "UGDS_2MOR        0.033900       0.5333  \n",
       "UGDS_NRA         0.011700       0.9286  \n",
       "UGDS_UNKN        0.045400       0.9027  \n",
       "PPTUG_EF         0.376900       1.0000  \n",
       "PCTPELL          0.712900       1.0000  \n",
       "PCTFLOAN         0.745000       1.0000  \n",
       "UG25ABV          0.572275       1.0000  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college['RELAFFIL'] = college['RELAFFIL'].astype(np.int8)\n",
    "college.describe(include=['int', 'float']).T # int在64位系统上默认是int64，所以RELAFFIL没有被include。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HBCU</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.014238</td>\n",
       "      <td>0.118478</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.009213</td>\n",
       "      <td>0.095546</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>WOMENONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005304</td>\n",
       "      <td>0.072642</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>0.190975</td>\n",
       "      <td>0.393096</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATVRMID</th>\n",
       "      <td>1185.0</td>\n",
       "      <td>522.819409</td>\n",
       "      <td>68.578862</td>\n",
       "      <td>290.0</td>\n",
       "      <td>475.000000</td>\n",
       "      <td>510.00000</td>\n",
       "      <td>555.000000</td>\n",
       "      <td>7.650000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SATMTMID</th>\n",
       "      <td>1196.0</td>\n",
       "      <td>530.765050</td>\n",
       "      <td>73.469767</td>\n",
       "      <td>310.0</td>\n",
       "      <td>482.000000</td>\n",
       "      <td>520.00000</td>\n",
       "      <td>565.000000</td>\n",
       "      <td>7.850000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DISTANCEONLY</th>\n",
       "      <td>7164.0</td>\n",
       "      <td>0.005583</td>\n",
       "      <td>0.074519</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>2356.837940</td>\n",
       "      <td>5474.275871</td>\n",
       "      <td>0.0</td>\n",
       "      <td>117.000000</td>\n",
       "      <td>412.50000</td>\n",
       "      <td>1929.500000</td>\n",
       "      <td>1.515580e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.510207</td>\n",
       "      <td>0.286958</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.267500</td>\n",
       "      <td>0.55570</td>\n",
       "      <td>0.747875</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.189997</td>\n",
       "      <td>0.224587</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.036125</td>\n",
       "      <td>0.10005</td>\n",
       "      <td>0.257700</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.161635</td>\n",
       "      <td>0.221854</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.027600</td>\n",
       "      <td>0.07140</td>\n",
       "      <td>0.198875</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.033544</td>\n",
       "      <td>0.073777</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.01290</td>\n",
       "      <td>0.032700</td>\n",
       "      <td>9.727000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.013813</td>\n",
       "      <td>0.070196</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00260</td>\n",
       "      <td>0.007300</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.004569</td>\n",
       "      <td>0.033125</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>9.983000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.023950</td>\n",
       "      <td>0.031288</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01750</td>\n",
       "      <td>0.033900</td>\n",
       "      <td>5.333000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.016086</td>\n",
       "      <td>0.050172</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.011700</td>\n",
       "      <td>9.286000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>6874.0</td>\n",
       "      <td>0.045181</td>\n",
       "      <td>0.093440</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.01430</td>\n",
       "      <td>0.045400</td>\n",
       "      <td>9.027000e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PPTUG_EF</th>\n",
       "      <td>6853.0</td>\n",
       "      <td>0.226639</td>\n",
       "      <td>0.246470</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.15040</td>\n",
       "      <td>0.376900</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CURROPER</th>\n",
       "      <td>7535.0</td>\n",
       "      <td>1328.063172</td>\n",
       "      <td>115201.552429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTPELL</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.530643</td>\n",
       "      <td>0.225544</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.357800</td>\n",
       "      <td>0.52150</td>\n",
       "      <td>0.712900</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PCTFLOAN</th>\n",
       "      <td>6849.0</td>\n",
       "      <td>0.522211</td>\n",
       "      <td>0.283616</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.332900</td>\n",
       "      <td>0.58330</td>\n",
       "      <td>0.745000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UG25ABV</th>\n",
       "      <td>6718.0</td>\n",
       "      <td>0.410021</td>\n",
       "      <td>0.228939</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.241500</td>\n",
       "      <td>0.40075</td>\n",
       "      <td>0.572275</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               count         mean            std    min         25%  \\\n",
       "HBCU          7164.0     0.014238       0.118478    0.0    0.000000   \n",
       "MENONLY       7164.0     0.009213       0.095546    0.0    0.000000   \n",
       "WOMENONLY     7164.0     0.005304       0.072642    0.0    0.000000   \n",
       "RELAFFIL      7535.0     0.190975       0.393096    0.0    0.000000   \n",
       "SATVRMID      1185.0   522.819409      68.578862  290.0  475.000000   \n",
       "SATMTMID      1196.0   530.765050      73.469767  310.0  482.000000   \n",
       "DISTANCEONLY  7164.0     0.005583       0.074519    0.0    0.000000   \n",
       "UGDS          6874.0  2356.837940    5474.275871    0.0  117.000000   \n",
       "UGDS_WHITE    6874.0     0.510207       0.286958    0.0    0.267500   \n",
       "UGDS_BLACK    6874.0     0.189997       0.224587    0.0    0.036125   \n",
       "UGDS_HISP     6874.0     0.161635       0.221854    0.0    0.027600   \n",
       "UGDS_ASIAN    6874.0     0.033544       0.073777    0.0    0.002500   \n",
       "UGDS_AIAN     6874.0     0.013813       0.070196    0.0    0.000000   \n",
       "UGDS_NHPI     6874.0     0.004569       0.033125    0.0    0.000000   \n",
       "UGDS_2MOR     6874.0     0.023950       0.031288    0.0    0.000000   \n",
       "UGDS_NRA      6874.0     0.016086       0.050172    0.0    0.000000   \n",
       "UGDS_UNKN     6874.0     0.045181       0.093440    0.0    0.000000   \n",
       "PPTUG_EF      6853.0     0.226639       0.246470    0.0    0.000000   \n",
       "CURROPER      7535.0  1328.063172  115201.552429    0.0    1.000000   \n",
       "PCTPELL       6849.0     0.530643       0.225544    0.0    0.357800   \n",
       "PCTFLOAN      6849.0     0.522211       0.283616    0.0    0.332900   \n",
       "UG25ABV       6718.0     0.410021       0.228939    0.0    0.241500   \n",
       "\n",
       "                    50%          75%           max  \n",
       "HBCU            0.00000     0.000000  1.000000e+00  \n",
       "MENONLY         0.00000     0.000000  1.000000e+00  \n",
       "WOMENONLY       0.00000     0.000000  1.000000e+00  \n",
       "RELAFFIL        0.00000     0.000000  1.000000e+00  \n",
       "SATVRMID      510.00000   555.000000  7.650000e+02  \n",
       "SATMTMID      520.00000   565.000000  7.850000e+02  \n",
       "DISTANCEONLY    0.00000     0.000000  1.000000e+00  \n",
       "UGDS          412.50000  1929.500000  1.515580e+05  \n",
       "UGDS_WHITE      0.55570     0.747875  1.000000e+00  \n",
       "UGDS_BLACK      0.10005     0.257700  1.000000e+00  \n",
       "UGDS_HISP       0.07140     0.198875  1.000000e+00  \n",
       "UGDS_ASIAN      0.01290     0.032700  9.727000e-01  \n",
       "UGDS_AIAN       0.00260     0.007300  1.000000e+00  \n",
       "UGDS_NHPI       0.00000     0.002500  9.983000e-01  \n",
       "UGDS_2MOR       0.01750     0.033900  5.333000e-01  \n",
       "UGDS_NRA        0.00000     0.011700  9.286000e-01  \n",
       "UGDS_UNKN       0.01430     0.045400  9.027000e-01  \n",
       "PPTUG_EF        0.15040     0.376900  1.000000e+00  \n",
       "CURROPER        1.00000     1.000000  1.000000e+07  \n",
       "PCTPELL         0.52150     0.712900  1.000000e+00  \n",
       "PCTFLOAN        0.58330     0.745000  1.000000e+00  \n",
       "UG25ABV         0.40075     0.572275  1.000000e+00  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.describe(include=['number']).T # number包含所有的数据类型，所以RELAFFIL又出现了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "college['MENONLY'] = college['MENONLY'].astype('float16')\n",
    "college['RELAFFIL'] = college['RELAFFIL'].astype('int8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60280"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.index = pd.Int64Index(college.index)\n",
    "college.index.memory_usage() # 7535 * 8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在最大值里选取最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>7.9</td>\n",
       "      <td>237000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>7.1</td>\n",
       "      <td>300000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>6.8</td>\n",
       "      <td>245000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>8.5</td>\n",
       "      <td>250000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>7.1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title  imdb_score       budget\n",
       "0                                      Avatar         7.9  237000000.0\n",
       "1    Pirates of the Caribbean: At World's End         7.1  300000000.0\n",
       "2                                     Spectre         6.8  245000000.0\n",
       "3                       The Dark Knight Rises         8.5  250000000.0\n",
       "4  Star Wars: Episode VII - The Force Awakens         7.1          NaN"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie2 = movie[['movie_title', 'imdb_score', 'budget']]\n",
    "movie2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2725</th>\n",
       "      <td>Towering Inferno</td>\n",
       "      <td>9.5</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1920</th>\n",
       "      <td>The Shawshank Redemption</td>\n",
       "      <td>9.3</td>\n",
       "      <td>25000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3402</th>\n",
       "      <td>The Godfather</td>\n",
       "      <td>9.2</td>\n",
       "      <td>6000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2779</th>\n",
       "      <td>Dekalog</td>\n",
       "      <td>9.1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>9.1</td>\n",
       "      <td>17000000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   movie_title  imdb_score      budget\n",
       "2725          Towering Inferno         9.5         NaN\n",
       "1920  The Shawshank Redemption         9.3  25000000.0\n",
       "3402             The Godfather         9.2   6000000.0\n",
       "2779                   Dekalog         9.1         NaN\n",
       "4312      Kickboxer: Vengeance         9.1  17000000.0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.nlargest(100, 'imdb_score').head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4804</th>\n",
       "      <td>Butterfly Girl</td>\n",
       "      <td>8.7</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4801</th>\n",
       "      <td>Children of Heaven</td>\n",
       "      <td>8.5</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>12 Angry Men</td>\n",
       "      <td>8.9</td>\n",
       "      <td>350000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4550</th>\n",
       "      <td>A Separation</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               movie_title  imdb_score    budget\n",
       "4804        Butterfly Girl         8.7  180000.0\n",
       "4801    Children of Heaven         8.5  180000.0\n",
       "4706          12 Angry Men         8.9  350000.0\n",
       "4550          A Separation         8.4  500000.0\n",
       "4636  The Other Dream Team         8.4  500000.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.nlargest(100, 'imdb_score').nsmallest(5, 'budget') # 在top100选取分数最低的5部"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在每个分组选取最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie2 = movie[['movie_title', 'title_year', 'imdb_score']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>imdb_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3884</th>\n",
       "      <td>The Veil</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>4.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2375</th>\n",
       "      <td>My Big Fat Greek Wedding 2</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>6.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2794</th>\n",
       "      <td>Miracles from Heaven</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>6.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>Independence Day: Resurgence</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>5.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>153</th>\n",
       "      <td>Kung Fu Panda 3</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>7.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       movie_title  title_year  imdb_score\n",
       "3884                      The Veil      2016.0         4.7\n",
       "2375    My Big Fat Greek Wedding 2      2016.0         6.1\n",
       "2794          Miracles from Heaven      2016.0         6.8\n",
       "92    Independence Day: Resurgence      2016.0         5.5\n",
       "153                Kung Fu Panda 3      2016.0         7.2"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.sort_values('title_year', ascending=False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>imdb_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>9.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4277</th>\n",
       "      <td>A Beginner's Guide to Snuff</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3798</th>\n",
       "      <td>Airlift</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>Captain America: Civil War</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>Godzilla Resurgence</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>8.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      movie_title  title_year  imdb_score\n",
       "4312         Kickboxer: Vengeance      2016.0         9.1\n",
       "4277  A Beginner's Guide to Snuff      2016.0         8.7\n",
       "3798                      Airlift      2016.0         8.5\n",
       "27     Captain America: Civil War      2016.0         8.2\n",
       "98            Godzilla Resurgence      2016.0         8.2"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie3 = movie2.sort_values(['title_year','imdb_score'], ascending=False) # 先按title_year，如果相等再按imdb_score排序。\n",
    "movie3.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>imdb_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>9.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3745</th>\n",
       "      <td>Running Forever</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>8.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4369</th>\n",
       "      <td>Queen of the Mountains</td>\n",
       "      <td>2014.0</td>\n",
       "      <td>8.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3935</th>\n",
       "      <td>Batman: The Dark Knight Returns, Part 2</td>\n",
       "      <td>2013.0</td>\n",
       "      <td>8.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title  title_year  imdb_score\n",
       "4312                     Kickboxer: Vengeance      2016.0         9.1\n",
       "3745                          Running Forever      2015.0         8.6\n",
       "4369                   Queen of the Mountains      2014.0         8.7\n",
       "3935  Batman: The Dark Knight Returns, Part 2      2013.0         8.4\n",
       "3                       The Dark Knight Rises      2012.0         8.5"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_top_year = movie3.drop_duplicates(subset='title_year') # 每个年份只保留一条记录\n",
    "movie_top_year.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>title_year</th>\n",
       "      <th>content_rating</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4026</th>\n",
       "      <td>Compadres</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>R</td>\n",
       "      <td>3000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4658</th>\n",
       "      <td>Fight to the Finish</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>150000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4661</th>\n",
       "      <td>Rodeo Girl</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>PG</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3252</th>\n",
       "      <td>The Wailing</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>Not Rated</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4659</th>\n",
       "      <td>Alleluia! The Devil's Carnival</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4731</th>\n",
       "      <td>Bizarre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>Unrated</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>812</th>\n",
       "      <td>The Ridiculous 6</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>TV-14</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4831</th>\n",
       "      <td>The Gallows</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>R</td>\n",
       "      <td>100000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4825</th>\n",
       "      <td>Romantic Schemer</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>125000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3796</th>\n",
       "      <td>R.L. Stine's Monsterville: The Cabinet of Souls</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG</td>\n",
       "      <td>4400000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          movie_title  title_year  \\\n",
       "4026                                        Compadres      2016.0   \n",
       "4658                              Fight to the Finish      2016.0   \n",
       "4661                                       Rodeo Girl      2016.0   \n",
       "3252                                      The Wailing      2016.0   \n",
       "4659                   Alleluia! The Devil's Carnival      2016.0   \n",
       "4731                                          Bizarre      2015.0   \n",
       "812                                  The Ridiculous 6      2015.0   \n",
       "4831                                      The Gallows      2015.0   \n",
       "4825                                 Romantic Schemer      2015.0   \n",
       "3796  R.L. Stine's Monsterville: The Cabinet of Souls      2015.0   \n",
       "\n",
       "     content_rating     budget  \n",
       "4026              R  3000000.0  \n",
       "4658          PG-13   150000.0  \n",
       "4661             PG   500000.0  \n",
       "3252      Not Rated        NaN  \n",
       "4659            NaN   500000.0  \n",
       "4731        Unrated   500000.0  \n",
       "812           TV-14        NaN  \n",
       "4831              R   100000.0  \n",
       "4825          PG-13   125000.0  \n",
       "3796             PG  4400000.0  "
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie4 = movie[['movie_title', 'title_year', 'content_rating', 'budget']]\n",
    "movie4_sorted = movie4.sort_values(['title_year', 'content_rating', 'budget'], \n",
    "                                   ascending=[False, False, True])\n",
    "movie4_sorted.drop_duplicates(subset=['title_year', 'content_rating']).head(10) # title_year加content_rating的组合只保留一条记录"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用sort_values替换nlargest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4804</th>\n",
       "      <td>Butterfly Girl</td>\n",
       "      <td>8.7</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4801</th>\n",
       "      <td>Children of Heaven</td>\n",
       "      <td>8.5</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>12 Angry Men</td>\n",
       "      <td>8.9</td>\n",
       "      <td>350000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4550</th>\n",
       "      <td>A Separation</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               movie_title  imdb_score    budget\n",
       "4804        Butterfly Girl         8.7  180000.0\n",
       "4801    Children of Heaven         8.5  180000.0\n",
       "4706          12 Angry Men         8.9  350000.0\n",
       "4550          A Separation         8.4  500000.0\n",
       "4636  The Other Dream Team         8.4  500000.0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "movie2 = movie[['movie_title', 'imdb_score', 'budget']]\n",
    "movie_smallest_largest = movie2.nlargest(100, 'imdb_score').nsmallest(5, 'budget')\n",
    "movie_smallest_largest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2725</th>\n",
       "      <td>Towering Inferno</td>\n",
       "      <td>9.5</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1920</th>\n",
       "      <td>The Shawshank Redemption</td>\n",
       "      <td>9.3</td>\n",
       "      <td>25000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3402</th>\n",
       "      <td>The Godfather</td>\n",
       "      <td>9.2</td>\n",
       "      <td>6000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2779</th>\n",
       "      <td>Dekalog</td>\n",
       "      <td>9.1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4312</th>\n",
       "      <td>Kickboxer: Vengeance</td>\n",
       "      <td>9.1</td>\n",
       "      <td>17000000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   movie_title  imdb_score      budget\n",
       "2725          Towering Inferno         9.5         NaN\n",
       "1920  The Shawshank Redemption         9.3  25000000.0\n",
       "3402             The Godfather         9.2   6000000.0\n",
       "2779                   Dekalog         9.1         NaN\n",
       "4312      Kickboxer: Vengeance         9.1  17000000.0"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.sort_values('imdb_score', ascending=False).head(100).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4815</th>\n",
       "      <td>A Charlie Brown Christmas</td>\n",
       "      <td>8.4</td>\n",
       "      <td>150000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4801</th>\n",
       "      <td>Children of Heaven</td>\n",
       "      <td>8.5</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4804</th>\n",
       "      <td>Butterfly Girl</td>\n",
       "      <td>8.7</td>\n",
       "      <td>180000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>12 Angry Men</td>\n",
       "      <td>8.9</td>\n",
       "      <td>350000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    movie_title  imdb_score    budget\n",
       "4815  A Charlie Brown Christmas         8.4  150000.0\n",
       "4801         Children of Heaven         8.5  180000.0\n",
       "4804             Butterfly Girl         8.7  180000.0\n",
       "4706               12 Angry Men         8.9  350000.0\n",
       "4636       The Other Dream Team         8.4  500000.0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据imdb_score选取前100条，然后再按budget排序。\n",
    "movie2.sort_values('imdb_score', ascending=False).head(100).sort_values('budget').head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4023</th>\n",
       "      <td>Oldboy</td>\n",
       "      <td>8.4</td>\n",
       "      <td>3000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4163</th>\n",
       "      <td>To Kill a Mockingbird</td>\n",
       "      <td>8.4</td>\n",
       "      <td>2000000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4395</th>\n",
       "      <td>Reservoir Dogs</td>\n",
       "      <td>8.4</td>\n",
       "      <td>1200000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4550</th>\n",
       "      <td>A Separation</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                movie_title  imdb_score     budget\n",
       "4023                 Oldboy         8.4  3000000.0\n",
       "4163  To Kill a Mockingbird         8.4  2000000.0\n",
       "4395         Reservoir Dogs         8.4  1200000.0\n",
       "4550           A Separation         8.4   500000.0\n",
       "4636   The Other Dream Team         8.4   500000.0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.nlargest(100, 'imdb_score').tail() # nlargest是排序的，而且这么写代码更精简。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>budget</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3799</th>\n",
       "      <td>Anne of Green Gables</td>\n",
       "      <td>8.4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3777</th>\n",
       "      <td>Requiem for a Dream</td>\n",
       "      <td>8.4</td>\n",
       "      <td>4500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3935</th>\n",
       "      <td>Batman: The Dark Knight Returns, Part 2</td>\n",
       "      <td>8.4</td>\n",
       "      <td>3500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4636</th>\n",
       "      <td>The Other Dream Team</td>\n",
       "      <td>8.4</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2455</th>\n",
       "      <td>Aliens</td>\n",
       "      <td>8.4</td>\n",
       "      <td>18500000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title  imdb_score      budget\n",
       "3799                     Anne of Green Gables         8.4         NaN\n",
       "3777                      Requiem for a Dream         8.4   4500000.0\n",
       "3935  Batman: The Dark Knight Returns, Part 2         8.4   3500000.0\n",
       "4636                     The Other Dream Team         8.4    500000.0\n",
       "2455                                   Aliens         8.4  18500000.0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.sort_values('imdb_score', ascending=False).head(100).tail() # 排序后tail一定是分数最低的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算止损价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas_datareader as pdr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Open</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Adj Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>84.258003</td>\n",
       "      <td>80.416000</td>\n",
       "      <td>81.000000</td>\n",
       "      <td>83.666000</td>\n",
       "      <td>51428500.0</td>\n",
       "      <td>83.666000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>86.139999</td>\n",
       "      <td>84.342003</td>\n",
       "      <td>84.900002</td>\n",
       "      <td>86.052002</td>\n",
       "      <td>47660500.0</td>\n",
       "      <td>86.052002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>90.800003</td>\n",
       "      <td>87.384003</td>\n",
       "      <td>88.099998</td>\n",
       "      <td>88.601997</td>\n",
       "      <td>88892500.0</td>\n",
       "      <td>88.601997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-06</th>\n",
       "      <td>90.311996</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>88.094002</td>\n",
       "      <td>90.307999</td>\n",
       "      <td>50665000.0</td>\n",
       "      <td>90.307999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-07</th>\n",
       "      <td>94.325996</td>\n",
       "      <td>90.671997</td>\n",
       "      <td>92.279999</td>\n",
       "      <td>93.811996</td>\n",
       "      <td>89410500.0</td>\n",
       "      <td>93.811996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-08</th>\n",
       "      <td>99.697998</td>\n",
       "      <td>93.646004</td>\n",
       "      <td>94.739998</td>\n",
       "      <td>98.428001</td>\n",
       "      <td>155721500.0</td>\n",
       "      <td>98.428001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-09</th>\n",
       "      <td>99.760002</td>\n",
       "      <td>94.573997</td>\n",
       "      <td>99.419998</td>\n",
       "      <td>96.267998</td>\n",
       "      <td>142202000.0</td>\n",
       "      <td>96.267998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-10</th>\n",
       "      <td>96.987999</td>\n",
       "      <td>94.739998</td>\n",
       "      <td>96.358002</td>\n",
       "      <td>95.629997</td>\n",
       "      <td>64797500.0</td>\n",
       "      <td>95.629997</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 High        Low       Open      Close       Volume  Adj Close\n",
       "Date                                                                          \n",
       "2019-12-31  84.258003  80.416000  81.000000  83.666000   51428500.0  83.666000\n",
       "2020-01-02  86.139999  84.342003  84.900002  86.052002   47660500.0  86.052002\n",
       "2020-01-03  90.800003  87.384003  88.099998  88.601997   88892500.0  88.601997\n",
       "2020-01-06  90.311996  88.000000  88.094002  90.307999   50665000.0  90.307999\n",
       "2020-01-07  94.325996  90.671997  92.279999  93.811996   89410500.0  93.811996\n",
       "2020-01-08  99.697998  93.646004  94.739998  98.428001  155721500.0  98.428001\n",
       "2020-01-09  99.760002  94.573997  99.419998  96.267998  142202000.0  96.267998\n",
       "2020-01-10  96.987999  94.739998  96.358002  95.629997   64797500.0  95.629997"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla = pdr.DataReader('tsla', data_source='yahoo',start='2020-1-1') # 从yahoo获取tsla股价，可以使用Tushare获取A股数据替代。\n",
    "tsla.head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "tsla_close = tsla['Close'] # 收盘价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2019-12-31    83.666000\n",
       "2020-01-02    86.052002\n",
       "2020-01-03    88.601997\n",
       "2020-01-06    90.307999\n",
       "2020-01-07    93.811996\n",
       "2020-01-08    98.428001\n",
       "2020-01-09    98.428001\n",
       "2020-01-10    98.428001\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla_cummax = tsla_close.cummax() # 累计最高价\n",
    "tsla_cummax.head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2019-12-31    75.299400\n",
       "2020-01-02    77.446802\n",
       "2020-01-03    79.741798\n",
       "2020-01-06    81.277199\n",
       "2020-01-07    84.430797\n",
       "2020-01-08    88.585201\n",
       "2020-01-09    88.585201\n",
       "2020-01-10    88.585201\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tsla_trailing_stop = tsla_cummax * .9 # 高点打9折止损\n",
    "tsla_trailing_stop.head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "def set_trailing_loss(symbol, purchase_date, perc): # 根据买入日设置止损价\n",
    "    close = pdr.DataReader(symbol, 'yahoo', start=purchase_date)['Close']\n",
    "    return close.cummax() * perc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2020-06-01    155.405502\n",
       "2020-06-02    157.173503\n",
       "2020-06-03    157.556001\n",
       "2020-06-04    157.556001\n",
       "2020-06-05    159.119997\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "msft_trailing_stop = set_trailing_loss('msft', '2020-6-1', .85) # 20/6/1买入msft，85折止损。\n",
    "msft_trailing_stop.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
